<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleCheck"
          v-hasPermi="['quality:firstPartRecheck:recheck']"
        >
          复核
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport('#firstPartRecheck', '品管首件复核作业')"
        >
          导出
        </el-button>
      </el-col>

      <!-- 分页效果 -->
      <el-col :span="2.2" style="position: absolute; right: 0px; top: 0">
        <Page
          style="margin-right: 0px"
          :current="queryParams.pageNum"
          :page-size="queryParams.pageSize"
          :total="total"
          @on-change="pageChange"
          simple
        ></Page>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      id="firstPartRecheck"
      highlight-current-row
      border
      stripe
      :height="tableH"
      v-loading="loading"
      :data="firstPartRecheckList"
      @row-click="handleClick"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column
        fixed="left"
        label="#"
        type="index"
        width="40"
        align="center"
      />
      <el-table-column
        label="机台"
        :show-overflow-tooltip="true"
        width="80"
        align="center"
        prop="sjlJtbh"
      />
      <el-table-column
        label="制造单号"
        :show-overflow-tooltip="true"
        width="120"
        align="center"
        prop="moeZzdh"
      />
      <el-table-column
        label="销售单号"
        :show-overflow-tooltip="true"
        width="120"
        align="center"
        prop="moeSodh"
      />
      <el-table-column
        label="物料代码"
        :show-overflow-tooltip="true"
        width="180"
        align="center"
        prop="sjlWldm"
      />
      <el-table-column
        label="品名规格"
        :show-overflow-tooltip="true"
        width="250"
        align="center"
        prop="itmPmgg"
      />
      <el-table-column
        label="模具编号"
        :show-overflow-tooltip="true"
        width="180"
        align="center"
        prop="moeMjbh"
      />
      <el-table-column
        label="模具名称"
        :show-overflow-tooltip="true"
        width="220"
        align="center"
        prop="mjmMjmc"
      />
      <el-table-column
        label="穴数"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="moeXs"
      />
      <el-table-column
        label="判退原因"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="sjlYymc"
      />
      <el-table-column
        label="发出人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="sjlJlrymc"
      />
      <el-table-column
        label="判断人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="sjlPdrymc"
      />
      <el-table-column
        label="开始时间"
        :show-overflow-tooltip="true"
        align="center"
        prop="sjlKssj"
        width="150"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.sjlKssj, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="结束时间"
        :show-overflow-tooltip="true"
        align="center"
        prop="sjlJssj"
        width="150"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.sjlJssj, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="用时（h）"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="sjlGs"
      />
    </el-table>

    <!-- 查询对话框   -->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="400px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="100px"
      >
        <el-col :span="24">
          <el-form-item label="发生日期">
            <el-date-picker
              v-model="dateRangeSjlList"
              size="small"
              style="width: 215px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="物料代码" prop="sjlWldm">
            <el-input
              v-model="queryParams.sjlWldm"
              placeholder="请输入物料代码"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="机台编号" prop="sjlJtbh">
            <el-input
              v-model="queryParams.sjlJtbh"
              placeholder="请输入机台编号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>

    <!-- 复核对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      :close-on-click-modal="false"
      :rule="rules"
      draggable
      width="680px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-col :span="24">
          <el-form-item label="机台编号" prop="sjlJtbh">
            <el-input disabled style="width: 500px" v-model="form.sjlJtbh" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="物料代码" prop="sjlWldm">
            <el-input disabled style="width: 200px" v-model="form.sjlWldm" />
            <el-input disabled style="width: 300px" v-model="form.itmPmgg" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="不良原因" prop="sjlYymc">
            <el-input disabled style="width: 500px" v-model="form.sjlYymc" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="复核原因描述" prop="sjlFpdesc">
            <el-input
              type="textarea"
              style="width: 500px"
              v-model="form.sjlFpdesc"
            />
          </el-form-item>
        </el-col>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { exportTable } from "@/utils/ruoyi";
import {
  listFirstPartRecheck,
  updateFirstPartRecheck,
} from "@/api/quality/firstPart";

export default {
  name: "FirstPartRecheck",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 点击获取id
      sjlId: null,
      // 非单个禁用
      single: true,
      // 显示搜索条件
      showSearch: true,
      openSearch: false,
      // 总条数
      total: 0,
      tableH: document.body.clientHeight - 300,
      // 品检首件复核作业数据
      firstPartRecheckList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: document.body.clientHeight > 900 ? 25 : 17,
        sjlWldm: null,
        sjlJtbh: null,
        params: null,
      },
      // 发生日期时间范围
      dateRangeSjlList: [
        new Date(new Date().getTime() - 1000 * 60 * 60 * 24 * 30 * 3),
        new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 30),
      ],
      // 表单参数
      form: {
        sjlFpdesc: "限度接受！可生产。",
      },
      // 表单校验
      rules: {
        sjlFpdesc: [
          { required: true, message: "复核原因描述不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询品管复核作业列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.dateRangeSjlList && "" != this.dateRangeSjlList) {
        this.queryParams.params["beginSjlTime"] = this.moment(
          this.dateRangeSjlList[0]
        ).format("YYYY-MM-DD HH:mm:ss");
        this.queryParams.params["endSjlTime"] = this.moment(
          this.dateRangeSjlList[1]
        ).format("YYYY-MM-DD HH:mm:ss");
      }
      listFirstPartRecheck(this.queryParams).then((response) => {
        this.firstPartRecheckList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.queryParams.sjlJtbh = null;
      this.queryParams.sjlWldm = null;
      this.open = false;
      this.openSearch = false;
    },
    // 表单重置
    reset() {
      this.form = {
        sjlJtbh: null,
        moeZzdh: null,
        moeSodh: null,
        itmCustwldm: null,
        sjlWldm: null,
        itmPmgg: null,
        moeMjbh: null,
        mjmMjmc: null,
        moeXs: null,
        sjlYymc: null,
        sjlFpdesc: "限度接受！可生产。",
        itmSkzl: null,
        itmJz: null,
        itmMz: null,
        sjlJlrymc: null,
        sjlPdrymc: null,
        sjlKssj: null,
        sjlJssj: null,
        sjlGs: null,
        sjlId: null,
        sjlGlid: null,
      };
      this.resetForm("form");
    },
    /** 查询按钮操作 */
    handleSearch() {
      this.title = "查询品管复核作业";
      this.openSearch = true;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 点击表事件 */
    handleClick(row) {
      this.form.sjlId = row.sjlId;
      this.form.sjlJtbh = row.sjlJtbh;
      this.form.sjlWldm = row.sjlWldm;
      this.form.itmPmgg = row.itmPmgg;
      this.form.sjlYymc = row.sjlYymc;
      this.form.sjlMoeid = row.sjlMoeid;
      this.single = false;
    },
    /** 导出按钮操作 */
    handleExport(a, b) {
      exportTable(a, b);
    },
    /** 复核按钮操作 */
    handleCheck() {
      this.title = "复核界面";
      this.open = true;
    },
    /** 复核按钮确认事件 */
    submitForm() {
      updateFirstPartRecheck(this.form).then((res) => {
        if (res.code == 200) {
          this.$message.success("复核成功！");
        } else {
          this.$message.error("复核失败！");
          console.log(res);
        }
        this.open = false;
        this.getList();
      });
    },
    /** 分页 **/
    pageChange(page) {
      this.queryParams.pageNum = page;
      this.getList();
    },
  },
};
</script>

<style lang="scss" scoped></style>
